package com.allen.retrofit2_rxjava2.base;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.app.NotificationManager;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout.LayoutParams;

import com.allen.retrofit2_rxjava2.R;
import com.allen.retrofit2_rxjava2.app.MyApp;
import com.allen.retrofit2_rxjava2.manager.AppManager;
import com.allen.retrofit2_rxjava2.network.utils.NetUtil;
import com.allen.retrofit2_rxjava2.utils.ToastUtils;

import butterknife.ButterKnife;

/**
 * 所有Activity的基类
 *
 * @author 作者： Administrator
 */
@SuppressLint("ResourceAsColor")
public abstract class BaseActivity extends FragmentActivity {
    private final static String TAG = "BaseActivity";

    private NotificationManager nm;

    //常量定义，从10000开始，其他界面自己定义的从1000开始
    public final static int SHOW_TOAST = 10000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        AppManager appManager = AppManager.getAppManager();
        appManager.addActivity(this);
        appManager.setStartingActivity(false);
        init();
        //在Activity 类中绑定 ：ButterKnife.bind(this);必须在setContentView();之后绑定
        ButterKnife.bind(this);
        nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        data();
        listener();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        AppManager appManager = AppManager.getAppManager();
        appManager.removeActivity(this);
        if (appManager.currentActivity() == null && !appManager.isStartingActivity()) {
            nm.cancelAll();
            System.exit(0);
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putBoolean("isSystemReceiver", true);
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);

    }

    @Override
    protected void onPause() {
        super.onPause();

    }

    @Override
    protected void onStop() {
        super.onStop();

    }

    public ProgressDialog pDialog;

    public void initProgressDialog(String s, Activity context) {
        showProgressDialog(s, false);
    }

    public void showProgressDialog(String text, boolean cancelable) {
        closeProgressDialog();

        pDialog = initProgressDialog(this, ProgressDialog.STYLE_SPINNER, text,
                cancelable);
        if (cancelable) {
            pDialog.setOnCancelListener(new OnCancelListener() {
                @Override
                public void onCancel(DialogInterface arg0) {
                    onCancelProgressDialog();
                }
            });
        }

        pDialog.show();
    }

    public void closeProgressDialog() {
        if (pDialog != null && pDialog.isShowing()) {
            pDialog.dismiss();
        }
    }

    protected void onCancelProgressDialog() {
        closeProgressDialog();
    }

    public void initCountTimeProgressDialog(String s, Activity context,
                                            boolean autoSetOnCancelListener) {
        if (pDialog != null && pDialog.isShowing()) {
            pDialog.dismiss();
            // return;
        }
        pDialog = initProgressDialog(context, ProgressDialog.STYLE_SPINNER, s,
                true);
        if (autoSetOnCancelListener) {
            pDialog.setOnCancelListener(new OnCancelListener() {

                @Override
                public void onCancel(DialogInterface arg0) {
                    if (pDialog.isShowing()) {
                        pDialog.dismiss();
                    }
                }
            });
        }
        pDialog.show();

    }

    public void dimissProgress() {
        if (pDialog != null && pDialog.isShowing()) {
            pDialog.dismiss();
        }
    }

    public void setProgressDialogCancelListener(OnCancelListener listener) {
        pDialog.setOnCancelListener(listener);
    }

    /**
     * 布局初始化设置
     * 程序入库/UI控件注入 控件必须在init之后才有响应
     */
    public abstract void init();

    /**
     * 控件响应
     */
    public abstract void data();

    /**
     * 监听事件
     */
    public void listener() {

    }

    /**
     * Dialog 封装
     *
     * @param context
     * @param theme
     * @param layoutResID
     * @return mDialog
     */
    public static Dialog initDialog(Context context, int theme,
                                    int layoutResID) {
        Dialog mDialog = new Dialog(context, theme);
        mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        mDialog.setContentView(layoutResID);
        // mDialog.setTitle(titleName);
        return mDialog;
    }

    /**
     * ProgressDialog 封装
     *
     * @param context
     * @param style
     * @param message
     * @return mProgressDialog
     */
    public static ProgressDialog initProgressDialog(Context context, int style,
                                                    String message) {
        return initProgressDialog(context, style, message, false);
    }

    /**
     * ProgressDialog 封装
     *
     * @param context
     * @param style
     * @param message
     * @return mProgressDialog
     */
    public static ProgressDialog initProgressDialog(Context context, int style,
                                                    String message, boolean cancelable) {
        ProgressDialog mProgressDialog = new ProgressDialog(context);
        mProgressDialog.setCanceledOnTouchOutside(false);
        mProgressDialog.setProgressStyle(style);
        mProgressDialog.setMessage(message);
        mProgressDialog.setCancelable(cancelable);
        return mProgressDialog;
    }

    /**
     * @return 窗口宽度
     */
    public int getWindowWith() {
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        return dm.widthPixels;
    }

    /**
     * @return 窗口高度
     */
    public int getWindowHeight() {
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        return dm.heightPixels;
    }

    public void setLayoutParams(View v, int width, int height) {
        LayoutParams params = (LayoutParams) v.getLayoutParams();
        params.width = width;
        params.height = height;
        v.setLayoutParams(params);
    }

    public void setDialogWidth(Dialog dialog, int width) {
        WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
        lp.width = width;
        dialog.getWindow().setAttributes(lp);
    }

    public void setDialogHeight(Dialog dialog, int height) {
        WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
        lp.height = height;
        dialog.getWindow().setAttributes(lp);
    }

    public NotificationManager getNotificationManager() {
        return nm;
    }

    public void showToast(String content) {
        if (TextUtils.isEmpty(content)) {
            return;
        }
        ToastUtils.showToast(content);
    }

    public static void showToast(Object object) {
        if (object == null) {
            return;
        }
        ToastUtils.showToast(object.toString());
    }

    /**
     * 网络是否连接
     *
     * @return
     */
    public boolean isConnect() {
        if (NetUtil.isNetworkAvailable(MyApp.getContext())) {
            return true;
        }
        showToast(getResources().getString(R.string.check_the_network_connection));
        return false;
    }

    public float dp2px(int dp) {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
                getResources().getDisplayMetrics());
    }

    public void setBackgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.alpha = bgAlpha;
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        getWindow().setAttributes(layoutParams);
    }

}
